package com.lisa.passjava.question.service.impl;

import com.lisa.common.es.QuestionEsModel;
import com.lisa.common.utils.R;
import com.lisa.passjava.question.entity.TypeEntity;
import com.lisa.passjava.question.feign.SearchFeignService;
import com.lisa.passjava.question.service.TypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lisa.common.utils.PageUtils;
import com.lisa.common.utils.Query;

import com.lisa.passjava.question.dao.QuestionDao;
import com.lisa.passjava.question.entity.QuestionEntity;
import com.lisa.passjava.question.service.QuestionService;


@Service("questionService")
public class QuestionServiceImpl extends ServiceImpl<QuestionDao, QuestionEntity> implements QuestionService {

    @Autowired
    SearchFeignService searchFeignService;
    @Autowired
    TypeService typeService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<QuestionEntity> page = this.page(
                new Query<QuestionEntity>().getPage(params),
                new QueryWrapper<QuestionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public boolean saveQuestion(QuestionEntity questionEntity) {
        boolean saveResult = true;
        //saveResult = save(questionEntity);
        //保存到ES
        saveResult = saveEs(questionEntity);

        return true;
    }

    private boolean saveEs(QuestionEntity questionEntity) {
        //1、创建ESModel
        QuestionEsModel model = new QuestionEsModel();
        model.setId(1L);
        //2、复制属性（将questionEntity中的值复制到model中）
        BeanUtils.copyProperties(questionEntity,model);
        //获取题目类型的名称
        TypeEntity typeEntity = typeService.getById(questionEntity.getType());
        String typeName = typeEntity.getType();
        //给 ES model 的“类型”字段赋值
        model.setTypeName(typeName);
        System.out.println("----------------ESModel:" + model);

        // 3. 调用 passjava-search 服务，将数据发送到 ES 中保存。
        R r = searchFeignService.saveQuestion(model);
        System.out.println("r: " + r);
        return true;

    }

}